package com.jic.manager.controller;

import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.enums.ResponseCode;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.manager.model.vo.request.SysCategoryRequest;
import com.jic.manager.model.vo.response.SysCategoryResponse;
import com.jic.manager.model.entity.SysCategory;
import com.jic.manager.service.SysCategoryService;

import javax.annotation.Resource;

/**
 * @Author: admin
 * @date: 2020-11-25 17:41:47
 */
@RestController
@Slf4j
@RequestMapping("/sysCategory")
@Api(tags = " [文章分类]控制类-PC")
public class SysCategoryController {


    @Resource
    private SysCategoryService service;


    /**
     * 新增记录
     *
     * @param request 新增[文章分类]请求参数
     * @return SysCategoryResponse 新增[文章分类]响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[文章分类]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody SysCategoryRequest request) throws Throwable {
        SysCategory sysCategory = new SysCategory();
        for (SysCategory vshow : request.getVshow()) {
            for (SysCategory category1 : request.getCategory()) {
                request.setParentId(category1.getId());
            }
            request.setName(vshow.getName());
            BeanUtils.copyProperties(request, sysCategory);
            long flag = service.save(sysCategory);
            return RestResult.success(flag);
        }
        return null;
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[文章分类]请求参数
     * @return SysCategoryResponse 新增或修改[文章分类]响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[文章分类]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody SysCategoryRequest request) throws Throwable {
        SysCategory sysCategory = new SysCategory();
        BeanUtils.copyProperties(request, sysCategory);
        long flag = service.saveOrUpdate(sysCategory);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[文章分类]请求参数
     * @return SysCategoryResponse 删除[文章分类]响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[文章分类]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody SysCategoryRequest request) throws Throwable {
        SysCategory sysCategory = new SysCategory();
        BeanUtils.copyProperties(request, sysCategory);
        long flag = service.deleteByPrimaryKeyLogically(sysCategory);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[文章分类]请求参数
     * @return SysCategoryResponse 修改[文章分类]响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[文章分类]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody SysCategoryRequest request) throws Throwable {
        SysCategory sysCategory = new SysCategory();
        BeanUtils.copyProperties(request, sysCategory);
        long flag = service.updateByPrimaryKeySelective(sysCategory);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[文章分类]详情请求参数
     * @return SysCategoryResponse 查询[文章分类]详情响应参数
     * author admin
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[文章分类]详情")
    @ResponseBody
    public RestResult<SysCategoryResponse> getByPrimaryKey(@Validated @RequestBody SysCategoryRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            SysCategory sysCategory = new SysCategory();
            BeanUtils.copyProperties(request, sysCategory);
            SysCategory result = service.getByPrimaryKey(sysCategory);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            SysCategoryResponse bo = new SysCategoryResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new SysCategoryResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<SysCategory>> listPage(@RequestBody SysCategoryRequest request) throws Throwable {
        SysCategory param = new SysCategory();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        param.setParentId((long) 0);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }


    /**
     * SYS_CATEGORY 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [文章分类] ")
    @ResponseBody
    public RestResult<List<SysCategoryResponse>> listSysCategoryAllRecord(@RequestBody SysCategoryRequest request) throws Throwable {
        SysCategory param = new SysCategory();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<SysCategory> list = service.listAllRecord(param);
        List<SysCategoryResponse> responseList = list.stream().map(temp -> {
            SysCategoryResponse bo = new SysCategoryResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

}

	
